package com.software.course.controller;

import com.software.course.enity.Course;
import com.software.course.enity.User;
import com.software.course.enity.query.TeacherQuery;
import com.software.course.service.TeacherService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/teacherRole")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @GetMapping("/basic_table")
    public String basic_table(){
        return "tables/basic_table";
    }

    @GetMapping("/stuInfo.html")
    public String stuInfo(Model model,TeacherQuery teacherQuery){
//        PageInfo<User> userPageInfo = teacherService.liseStuByName(teacherQuery);
//        model.addAttribute("stuPages",userPageInfo);
        List<User> users = teacherService.listStu();
        model.addAttribute("users",users);
        return "tables/stuInfo";
    }

    @GetMapping("/courseInfo.html")
    public String courseInfo(Model model){
        List<Course> courses = teacherService.listCourse();
        model.addAttribute("courses",courses);
        return "tables/courseInfo";
    }

//    @GetMapping("/editable_table")
//    public String editable_table(){
//        return "editCourseInfo";
//    }

    @GetMapping("/responsive_table")
    public String responsive_table(){
        return "tables/responsive_table";
    }

    @GetMapping("/downloadFile/{id}")
    public String download(@PathVariable("id") Integer id, HttpServletResponse response, RedirectAttributes attributes) throws IOException {
          com.software.course.enity.File file =  teacherService.getFileByUserId(id);
          if(file == null){
              System.out.println("未提交作业");
              attributes.addFlashAttribute("message","该学生未提交作业");
              return "redirect:/teacherRole/stuInfo.html";
          }
          String fileName = file.getFileName();
            try {
                  String realPath = ResourceUtils.getURL("classpath:").getPath() + "/static/files";
                  FileInputStream inputStream = new FileInputStream(new File(realPath, fileName));
                  response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
                  //3、获取响应输出流
                  PrintWriter writer = response.getWriter();
                  //4、流数据交换
                  IOUtils.copy(inputStream, writer);
                  //5、关闭资源
                  IOUtils.closeQuietly(inputStream);
                  IOUtils.closeQuietly(writer);

          }catch (IOException e){
              System.out.println("yichang");
          }
        return "redirect:/teacherRole/stuInfo.html";
    }


}
